using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.RequestDto;
using TMS.Domain.Entity;

namespace TMS.Api.Controllers
{
    [ApiController]
    //辅导员
    public class InstructorController : ControllerBase
    {
        private readonly IRepository<Students> _Students;
        private readonly IRepository<Subject> _Subject;//科目表
        private readonly IRepository<ClassTeacher> _ClassTeacher;//教室教师表(班级教师表)

        private readonly IRepository<Class> _Class;//班级表
        private readonly IRepository<Major> _Major;//专业表
        private readonly IRepository<College> _College;//学院表
        private readonly IRepository<Mark> _Mark;//成绩表

        public InstructorController(IRepository<Mark> Mark,IRepository<Major> Major, IRepository<College> College, IRepository<ClassTeacher> ClassTeacher, IRepository<Students> Students, IRepository<Subject> Subject, IRepository<Class> Class)
        {
            _Students = Students;
            _Subject = Subject;
            _Class = Class;
            _ClassTeacher = ClassTeacher;
            _College = College;
            _Major = Major;
            _Mark=Mark;
        }

        //花名册===>通过班级id进行学生的查询
        [HttpGet("/GetClassInfos/{ClassId}")]
        public object GetClassInfo(Guid ClassId)
        {
            //需要的是该班级里的学生的学号、姓名、性别、院系专业以及该课程的成绩
            var Student = _Students.Table.ToList();
            var classes = _Class.Table.ToList();
            var Major = _Major.Table.ToList();
            var colleges = _College.Table.ToList();
            // var marks = _Mark.Table.ToList();//

            // var roster =Student.Where(x=>x.ClassId==ClassId).Select(x=>new{x.StudentId,x.StudentName}).ToList() ;//花名册
            var roster = Student.Where(x => x.ClassId == ClassId).OrderBy(x=>x.StudentId).ToList();//花名册
            object[] obj = new object[roster.Count()];

            if (roster.Count() != 0)
            {

                //一个班级一般都是同一个院系同一个专业(不考虑选修课)
                var isClass = classes.FirstOrDefault(x => x.Id == roster[0].ClassId);
                if (isClass != null)
                {//通过班级id在专业表获取专业名称和学院id
                    var ClassName = isClass.ClassName;
                    var majorInfo = Major.FirstOrDefault(x => x.Id == isClass.MajorId);
                    var majorName = majorInfo.MajorName;//专业名称
                    var collegeName = colleges.FirstOrDefault(x => x.Id == majorInfo.CollegeId).CollegeName;//学院名称
                    for (int i = 0; i < roster.Count(); i++)
                    {
                        obj[i] = new 
                        { 

                            roster[i].StudentId,
                            roster[i].StudentName,
                            roster[i].Sex,
                            ClassName,
                            majorName,
                            collegeName
                        };
                    }


                }
            }



            return new
            {
                code = 200,
                data = obj,
                msg = "查询成功"
            };
        }

        [HttpGet("/GetTeacherClass/{teacherId}")]
        public object GetTeacherClass(Guid teacherId)
        {//通过教师id获取该教师所教的班级
            var ClassTeacher = _ClassTeacher.Table.ToList();
            var clases = _Class.Table.ToList();
            var ClassTeacherI = ClassTeacher.Where(x => x.TeacherId == teacherId).ToList();
            object[] clasesInfo = new object[ClassTeacherI.Count()];
            for (int i = 0; i < ClassTeacherI.Count(); i++)
            {
                var classInfo = clases.FirstOrDefault(x => x.Id == ClassTeacherI[i].ClassId);
                if (classInfo != null)
                {
                    clasesInfo[i] = new
                    {
                        classInfo.Id,//班级id
                        classInfo.ClassName//班级名称
                    };
                }
            }

            return new
            {
                code = 200,
                msg = "获取成功",
                data = clasesInfo
            };
        }
        
        //教师课程（班级）管理
        [HttpPost("/PostTeacherClassa")]
        public async Task<object> PostTeacherClass(PostTeacherClassDto PostTeacherClassDto)
        {//给教师分配班级（添加）
            //需要教师id 和 班级id(班级教师表)
            var classTeachers = _ClassTeacher.Table.ToList();
            var isExise = classTeachers.FirstOrDefault(x=>x.TeacherId==PostTeacherClassDto.TeacherId&&x.ClassId==PostTeacherClassDto.ClassId);
            if (isExise!=null)
            {
                return new{
                    code=400,
                    msg="该教师已存在此班级",
                    data=""
                };
            }else
            {
                 var data =await _ClassTeacher.AddAsync(new ClassTeacher{
                    ClassId=PostTeacherClassDto.ClassId,
                    TeacherId=PostTeacherClassDto.TeacherId
                });

                return new
                {
                    code=200,
                    msg="添加成功",
                    data
                };
            }            
        }
        //查看教师所教的课程 教师号  教师名字  课程数量
        //某一个教师所教的课程的详情 课程id 各个课程名字
        //添加课程
        //删除课程
    }
}